﻿using System.Globalization;
using System.Linq;
using System.Web.Mvc;

using BLL.Services.Authors;
using BLL.Services.AutoCompleteService;
using BLL.Services.Books;
using BLL.Services.Users;

namespace Web.Areas.Admin.Controllers
{
    /// <summary>
    /// Shared features
    /// </summary>
    public partial class SharedController : Controller
    {
        private readonly IAutoCompleteService _autoCompleteService;

        private readonly IBookService _bookService;

        private readonly IAuthorsService _authorsService;

        private readonly IUsersService _usersService;

        /// <summary>
        /// Initializes a new instance of the <see cref="SharedController" /> class.
        /// </summary>
        /// <param name="autoCompleteService">The auto complete service.</param>
        /// <param name="bookService">The book service.</param>
        /// <param name="authorsService">The authors service.</param>
        /// <param name="usersService">The users service.</param>
        public SharedController(IAutoCompleteService autoCompleteService,
            IBookService bookService,
            IAuthorsService authorsService,
            IUsersService usersService)
        {
            _autoCompleteService = autoCompleteService;
            _bookService = bookService;
            _authorsService = authorsService;
            _usersService = usersService;
        }

        /// <summary>
        /// Empties this instance.
        /// </summary>
        public virtual ActionResult Empty()
        {
            return View(MVC.Admin.Shared.Empty());
        }

        /// <summary>
        /// Gets the authors.
        /// </summary>
        /// <param name="term">The term.</param>
        public virtual ActionResult GetAuthors(string term)
        {
            var result = _autoCompleteService.GetAuthors(term);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the authors for book.
        /// </summary>
        /// <param name="bookId">The book id.</param>
        public virtual ActionResult GetAuthorsForBook(int? bookId)
        {
            if (!bookId.HasValue)
                return new EmptyResult();

            var book = _bookService.GetBookById(bookId.Value);
            if (book == null)
            {
                return new EmptyResult();
            }
            var authors = _authorsService.GetAuthorsByIds(book.AuthorIds);
            return Json(
                authors.Select(
                    x => new SelectListItem
                        {
                            Text = x.FirstName + " " + x.LastName,
                            Value = x.AuthorId.ToString(CultureInfo.InvariantCulture)
                        }),
                JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the users.
        /// </summary>
        /// <param name="term">The term.</param>
        public virtual ActionResult GetUsers(string term)
        {
            var result = _autoCompleteService.GetUsers(term);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the user by id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        public virtual ActionResult GetUserById(int? userId)
        {
            if (!userId.HasValue)
            {
                return new EmptyResult();
            }
            var user = _usersService.GetUserById(userId.Value);
            if (user == null)
            {
                return new EmptyResult();
            }
            return Json(new { Id = user.UserId, Value = user.Email }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the books.
        /// </summary>
        /// <param name="term">The term.</param>
        public virtual ActionResult GetBooks(string term)
        {
            var result = _autoCompleteService.GetBooks(term);
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the book by id.
        /// </summary>
        /// <param name="bookId">The book id.</param>
        public virtual ActionResult GetBookById(int? bookId)
        {
            if (!bookId.HasValue)
            {
                return new EmptyResult();
            }
            var book = _bookService.GetBookById(bookId.Value);
            if (book == null)
            {
                return new EmptyResult();
            }
            return Json(new { Id = book.BookId, Value = book.Name }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the categories.
        /// </summary>
        /// <param name="term">The term.</param>
        public virtual ActionResult GetCategories(string term)
        {
            var result = _autoCompleteService.GetCategories(term);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}